package com.xjy.web.controller.system;

import java.util.List;
import java.util.Arrays;

import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.exception.CustomException;
import com.xjy.common.exception.UtilException;
import com.xjy.system.domain.SysContract;
import com.xjy.system.domain.SysContractResult;
import com.xjy.system.domain.bo.SysContractResultAddBo;
import com.xjy.system.domain.bo.SysContractResultEditBo;
import com.xjy.system.domain.bo.SysContractResultQueryBo;
import com.xjy.system.domain.vo.SysContractResultVo;
import com.xjy.system.domain.vo.SysTransactionUnitVo;
import com.xjy.system.repository.SysContractResultRepository;
import com.xjy.system.utils.CommonPage;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.xjy.common.annotation.RepeatSubmit;
import com.xjy.common.annotation.Log;
import com.xjy.common.core.controller.BaseController;
import com.xjy.common.core.domain.AjaxResult;
import com.xjy.common.enums.BusinessType;
import com.xjy.system.service.ISysContractResultService;
import com.xjy.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 合同结算项目信息Controller
 *
 * @author xjy
 * @date 2022-02-08
 */
@Api(value = "合同结算项目信息控制器", tags = {"合同结算项目信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/result")
public class SysContractResultController extends BaseController {

    private final ISysContractResultService iSysContractResultService;

    @Autowired
    private SysContractResultRepository sysContractResultRepository;

    /**
     * 查询合同结算项目信息列表
     */
    @ApiOperation("查询合同结算项目信息列表")
    @PreAuthorize("@ss.hasPermi('system:result:list')")
    @GetMapping("/list")
    public TableDataInfo<SysContractResultVo> list(@Validated SysContractResultQueryBo bo) {
        return iSysContractResultService.queryPageList(bo);
    }
	/**
	 * 查询合同结算项目信息选择列表
	 */
	@ApiOperation("查询合同结算项目信息列表")
	@PreAuthorize("@ss.hasPermi('system:result:list')")
	@GetMapping("/selectList")
	public List<SysContractResultVo> selectList(@Validated SysContractResultQueryBo bo) {
		return iSysContractResultService.selectList(bo);
	}

    /**
     * 导出合同结算项目信息列表
     */
    @ApiOperation("导出合同结算项目信息列表")
    @PreAuthorize("@ss.hasPermi('system:result:export')")
    @Log(title = "合同结算项目信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<SysContractResultVo> export(@Validated SysContractResultQueryBo bo) {
        List<SysContractResultVo> list = iSysContractResultService.queryList(bo);
        ExcelUtil<SysContractResultVo> util = new ExcelUtil<SysContractResultVo>(SysContractResultVo.class);
        return util.exportExcel(list, "合同结算项目信息");
    }

    /**
     * 获取合同结算项目信息详细信息
     */
    @ApiOperation("获取合同结算项目信息详细信息")
    @PreAuthorize("@ss.hasPermi('system:result:query')")
    @GetMapping("/{id}")
    public AjaxResult<SysContractResultVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iSysContractResultService.queryById(id));
    }

    /**
     * 新增合同结算项目信息
     */
    @ApiOperation("新增合同结算项目信息")
    @PreAuthorize("@ss.hasPermi('system:result:add')")
    @Log(title = "合同结算项目信息", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody SysContractResultAddBo bo) {
        return toAjax(iSysContractResultService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改合同结算项目信息
     */
    @ApiOperation("修改合同结算项目信息")
    @PreAuthorize("@ss.hasPermi('system:result:edit')")
    @Log(title = "合同结算项目信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping("/edit")
    public AjaxResult<Void> edit(@Validated @RequestBody SysContractResultEditBo bo) {
		SysContractResultVo sysContractResultVo = iSysContractResultService.queryById(bo.getId());
		if(sysContractResultVo.getAuditStatus()){
			throw new UtilException("该数据已经审核无法修改！");
		}
        return toAjax(iSysContractResultService.updateByEditBo(bo) ? 1 : 0);
    }

	/**
	 * 审核合同结算项目信息
	 */
	@ApiOperation("修改合同结算项目信息")
	@PreAuthorize("@ss.hasPermi('system:result:edit')")
	@Log(title = "合同结算项目信息", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping("/audit")
	public synchronized AjaxResult<Void> audit(@Validated @RequestBody SysContractResultEditBo bo) {

		return toAjax(iSysContractResultService.auditByEditBo(bo) ? 1 : 0);
	}

    /**
     * 删除合同结算项目信息
     */
    @ApiOperation("删除合同结算项目信息")
    @PreAuthorize("@ss.hasPermi('system:result:remove')")
    @Log(title = "合同结算项目信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iSysContractResultService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }



	/**
	 * 根据合同查询结算项目
	 */
	@ApiOperation("根据合同查询结算项目")
	@GetMapping("/contractResultByContractId/{id}")
	public List<SysContractResult> listByProj(@NotNull(message = "主键不能为空")
											@PathVariable("id") Long id) {
		return this.sysContractResultRepository.findAllByContractId(id);
	}

}
